

#include "HeaderFiles.h"
#include "comm_struct.h"
#include "uart_comm_hdl.h"


uint8_t CMD_STOP_SCAN[9]  = {0xA5, 0xA5, 0xA5, 0xA5, 0x00, 0x64, 0x00, 0x00, 0x64};
uint8_t CMD_START_SCAN[9] = {0XA5, 0XA5, 0XA5, 0XA5, 0X00, 0X63, 0X00, 0X00, 0X63};
uint8_t CMD_GET_ADDR[9]   = {0xA5, 0xA5, 0xA5, 0xA5, 0x00, 0x60, 0x00, 0x00, 0x60};
uint8_t CMD_GET_PARA[9]   = {0XA5, 0XA5, 0XA5, 0XA5, 0X00, 0X61, 0X00, 0X00, 0X61};

STR_GS2_HDL gs2Hdl[2] = {{.num = GS2_LEFT, .recbuf=&tof050UARTRecbuf, .sendbuf=&tof050UARTSendbuf},
						 {.num = GS2_RIGHT, .recbuf=&debugUARTRecbuf, .sendbuf=&debugUARTSendbuf}};

						 
STR_COMM_FRAME  uploadframeGS2Data[2];	
STR_COMM_FRAME  uploadframeGS2Param[2];							 
						 
static uint8_t gs2_data_temp[SCI_RECBUF_LEN] = {0};	
static uint8_t gs2_databuf_temp[512];

/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/
void GS2_comm_frame_init(void)
{

	uploadframeGS2Data[0].head_H = FRAME_HEAD_H;
	uploadframeGS2Data[0].head_L = FRAME_HEAD_L;
	uploadframeGS2Data[0].cmd_ID = ID_GS2_DATA_UPLOAD;
	uploadframeGS2Data[0].length = 0xFF;
	uploadframeGS2Data[0].databuf = &gs2Hdl[0].distance.byte[0];
	
	uploadframeGS2Data[1].head_H = FRAME_HEAD_H;
	uploadframeGS2Data[1].head_L = FRAME_HEAD_L;
	uploadframeGS2Data[1].cmd_ID = ID_GS2_DATA_UPLOAD;
	uploadframeGS2Data[1].length = 0xFF;
	uploadframeGS2Data[1].databuf = &gs2Hdl[1].distance.byte[0];
	
	uploadframeGS2Param[0].head_H = FRAME_HEAD_H;
	uploadframeGS2Param[0].head_L = FRAME_HEAD_L;
	uploadframeGS2Param[0].cmd_ID = ID_GS2_DATA_UPLOAD;
	uploadframeGS2Param[0].length = DATA_LEGTH_GS2_PARAM + 1;
	uploadframeGS2Param[0].databuf = &gs2Hdl[0].param.byte[0];
	
	uploadframeGS2Param[1].head_H = FRAME_HEAD_H;
	uploadframeGS2Param[1].head_L = FRAME_HEAD_L;
	uploadframeGS2Param[1].cmd_ID = ID_GS2_DATA_UPLOAD;
	uploadframeGS2Param[1].length = DATA_LEGTH_GS2_PARAM + 1;
	uploadframeGS2Param[1].databuf = &gs2Hdl[1].param.byte[0];
	
}

						 
/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint8_t gs2_CRC_check(STR_GS2_HDL *hdl, ENUM_GS2_FRAME_TYPE type)
{
	uint8_t check_sum = 0;

	if(type == DATA_FRAME)
	{
		check_sum = 0;
		for(uint16_t i=0;i<DATA_LENGTH_DISTANCE_FRAME;i++)
		{
			check_sum += hdl->distance.byte[i+4];
		}
		
		if(check_sum ==	hdl->distance.frame.checksum)
		{
			return 1; 				 
		}
		else
		{
			return 0;
		}	
	}
	else
	{
		for(uint16_t i=0;i<DATA_LENGTH_PARAM_FRAME;i++)
		{
			check_sum += hdl->param.byte[i+4];
		}
		
		if(check_sum ==	hdl->param.frame.checksum)
		{
			return 1; 				 
		}
		else
		{
			return 0;
		}		
	}
}


/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/

void send_cmd_to_GS2(STR_GS2_HDL *hdl, uint8_t *cmd_addr)
{
	ringBufWrite(hdl->sendbuf, (char *)&cmd_addr[0], 9);	
}


/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint8_t GS2_param_read(STR_GS2_HDL *hdl)
{
	
	uint8_t databuf[25] = {0};
	
	ringBufRead(hdl->recbuf, (char*)databuf, 25);
	
	if( (databuf[0] == 0xA5) &&   \
		(databuf[1] == 0xA5) &&   \
		(databuf[2] == 0xA5) &&   \
		(databuf[3] == 0xA5) &&   \
		(databuf[5] == 0x61))
	{
		for(uint8_t i=0;i<18;i++)
		{
			hdl->param.byte[i] = databuf[i];
		}
		
		if(gs2_CRC_check(hdl, PARAM_FRAME) == 1)
		{
			return 1;
		}
	}	
	
	return 0;
}


/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint8_t GS2_data_read(STR_GS2_HDL *hdl)
{
	uint16_t length = 0;
	
	length = ringBufRead(hdl->recbuf, (char*)gs2_data_temp, SCI_RECBUF_LEN);
	
	if(length == 0)
	{
		return 0;
	}
	
	if( (gs2_data_temp[0] == 0xA5) &&   \
		(gs2_data_temp[1] == 0xA5) &&   \
		(gs2_data_temp[2] == 0xA5) &&   \
		(gs2_data_temp[3] == 0xA5) &&   \
		(gs2_data_temp[5] == 0x63))
	{
	
		for(uint16_t i=0;i<(DATA_LENGTH_DISTANCE_FRAME+5);i++)
		{
			hdl->distance.byte[i] = gs2_data_temp[i];
		}
		
		if(gs2_CRC_check(hdl, DATA_FRAME) == 1)
		{
			return 1;
		}
	}
	
	return 0;
}



/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/

void GS2_data_analysis(STR_GS2_HDL *hdl)
{
	if(hdl->read_param_flg == 0)
	{
		send_cmd_to_GS2(hdl, CMD_STOP_SCAN);
		send_cmd_to_GS2(hdl, CMD_GET_PARA);
		
		if(GS2_param_read(hdl))
		{
			hdl->read_param_flg = 1;
			send_cmd_to_GS2(hdl, CMD_START_SCAN);
		}
	}
	
	else
	{
		if(GS2_data_read(hdl))
		{
			hdl->read_data_cnt++;
			
			hdl->read_data_send = 1;
			
			hdl->rece_time_stamp = (uint16_t)get_system_runingtime();
		}
	}
}

/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/

void GS2_frame_upload(STR_COMM_FRAME *frame, STR_RINGBUF* bufhdl)
{

	uint16_t frame_id    = 0;
	uint16_t crc_value   = 0;
	uint16_t data_length = frame->length - 1;
	uint16_t frame_length = frame->length;
	
	if(frame->length == 0xFF)
	{
		data_length = DATA_LEGTH_GS2_DATA;
		frame_length = DATA_LEGTH_GS2_DATA + 1;
	}
	
	uint8_t *databuf_p = frame->databuf;
	
	static uint16_t upload_cnt = 0;
	
	upload_cnt++;
	
	frame_id = upload_cnt;

	frame->frame_ID_H = ((frame_id >> 8) & 0x00FF);
	frame->frame_ID_L = (frame_id & 0x00FF);
	
	gs2_databuf_temp[0] = frame->cmd_ID;
	
	for(uint16_t i=1;i<(frame_length+1);i++)
	{
		gs2_databuf_temp[i] = *(databuf_p++);
	}
	
	crc_value = GetCRC((uint8_t*)(gs2_databuf_temp), frame_length);
	
	frame->CRC_H = ((crc_value >> 8) & 0x00FF);
	frame->CRC_L = (crc_value & 0x00FF);
	
	ringBufWrite(bufhdl, (char*)frame, 6);
	ringBufWrite(bufhdl, (char*)frame->databuf, data_length);
	ringBufWrite(bufhdl, (char*)(&frame->CRC_H), 1);
    ringBufWrite(bufhdl, (char*)(&frame->CRC_L), 1);
}

/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/
void GS2_wall_sensor_run(void)
{
	#if 1
	for(uint8_t i=0;i<2;i++)
	{
		gs2Hdl[i].data_end_cnt++;
		if(gs2Hdl[i].data_end_cnt > DATA_END_HOLD_TIME_MS)
		{			
			GS2_data_analysis(&gs2Hdl[i]);
		}
		
		if(gs2Hdl[i].read_data_send == 1)
		{
			uint16_t time_stamp_diff = 0;
			
			time_stamp_diff = (gs2Hdl[i].rece_time_stamp - gs2Hdl[i].store_time_stamp) + 0xFFFF;
			
			if(time_stamp_diff > GS2_SEND_PERIOD_LIMIT_MS)
			{
				if(i == 0)	    gs2Hdl[i].distance.frame.address |= 0x10;
				else if(i == 1) gs2Hdl[i].distance.frame.address |= 0x20;
				
				GS2_frame_upload(&uploadframeGS2Data[i], &commUARTSendbuf);
				gs2Hdl[i].read_data_send = 0;
				
				gs2Hdl[i].store_time_stamp = gs2Hdl[i].rece_time_stamp;
			}
			else
			{
				gs2Hdl[i].read_data_send = 0;
			}
		}
	}
	#endif	
	
	if(userHdl.assis.gs2_param_upload == 1)
	{
		gs2Hdl[0].param.frame.address |= 0x10;
		gs2Hdl[1].param.frame.address |= 0x20;
		GS2_frame_upload(&uploadframeGS2Param[0], &commUARTSendbuf);
		GS2_frame_upload(&uploadframeGS2Param[1], &commUARTSendbuf);
		userHdl.assis.gs2_param_upload = 0;
	}
}

/*!
    \brief      none
    \param[in]  none
    \param[out] none
    \retval     none
*/
